﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Meditime.UI
{

    public partial class CommandNotifier : Form
    {
        private static CommandNotifier _commandInstance;
        public static CommandNotifier Instance
        {
            set
            {
                _commandInstance = value;
            }
            get
            {
                if (_commandInstance == null)
                    _commandInstance = new CommandNotifier();
                return _commandInstance;
            }
        }


        public static void DisplayNotifier(Form mdiParent)
        {
            if (CommandNotifier.Instance.IsDisposed) CommandNotifier.Instance = null;
            CommandNotifier.Instance.MdiParent = mdiParent;
            CommandNotifier.Instance.Show();
            CommandNotifier.Instance.Top = 0;
            CommandNotifier.Instance.Height = mdiParent.Height - 63;
            CommandNotifier.Instance.Left = mdiParent.Width - (CommandNotifier.Instance.Width + 13);
        }

        public CommandNotifier()
        {
            InitializeComponent();
        }

        public void WriteToTextBox(string text, bool error = false)
        {
            try
            {

                if (error)
                    text = "+++ ERROR +++\r\n" + text;
                CommandNotifier.Instance.Output(this,
                    new RunWorkerCompletedEventArgs(text, null, false));
            }
            catch (Exception ex)
            {
                MessageBox.Show(Common.FormatError(ex));
            }
        }

        delegate void OutputMethod(object sender, RunWorkerCompletedEventArgs e);
        public void Output(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                if (txtNotification.InvokeRequired)
                {
                    txtNotification.Invoke(new OutputMethod(Output), new object[] { sender, e });
                    return;
                }

                string message = "";
                message += string.Format("{0}{1}", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), "\r\n");
                message += string.Format("{0}{1}", (string)e.Result, "\r\n");
                message += "------------\r\n";

                txtNotification.Text += message;
                CommandHistory.Instance.Add(message);

                txtNotification.SelectionStart = txtNotification.Text.Length;
                txtNotification.ScrollToCaret();
            }
            catch (Exception ex)
            {
                MessageBox.Show(Common.FormatError(ex));
            }
        }

        private void CommandNotifier_Load(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtNotification.Text))
                    // See if there is anything in the history, in case user has closed form.
                    CommandHistory.Instance.ForEach(p => txtNotification.Text += p);
                txtNotification.SelectionStart = txtNotification.Text.Length;
                txtNotification.ScrollToCaret();
            }
            catch { }
        }

    }
}
